package com.xb.cloud.order.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.order.model.PaymentFlow;
import com.xb.cloud.order.model.PaymentFlowExample;
import com.xb.cloud.order.model.mapper.PaymentFlowMapper;
import com.xb.cloud.order.service.client.PaymentFlowService;
import com.xb.cloud.order.service.client.bo.PaymentFlowBo;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class PaymentFlowServiceImpl extends AbstractService implements PaymentFlowService {
	@Resource
	PaymentFlowMapper paymentFlowMapper;

	@Override
	public ServiceReturnInfo createPaymentFlow(PaymentFlowBo paymentFlowBo) {
		PaymentFlow paymentFlow = BeanUtils.newAndCopyProperties(PaymentFlow.class, paymentFlowBo);
		paymentFlow.setId(ipKeyGenerator.generateKey());
		paymentFlow.setCreateTime(timeService.getDate());
		int insertSelective = paymentFlowMapper.insertSelective(paymentFlow);
		if (insertSelective > 0) {
			return ServiceReturnUtils.success();
		}

		return ServiceReturnUtils.error(ServiceReturnCodes.PAYMENT_FLOW_CREATE_ERROR);
	}

	@Override
	public ServiceReturnInfo getPaymentFlow(PaymentFlowBo paymentFlowBo) {
		PaymentFlowExample example = new PaymentFlowExample();
		example.createCriteria().andOutNoEqualTo(paymentFlowBo.getOutNo()).andSoIdEqualTo(paymentFlowBo.getSoId());
		List<PaymentFlow> paymentFlows = paymentFlowMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(paymentFlows)) {
			return ServiceReturnUtils.success(paymentFlows.get(0));
		}
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo updateStatus(PaymentFlowBo paymentFlowBo) {
		PaymentFlow paymentFlow = BeanUtils.newAndCopyProperties(PaymentFlow.class, paymentFlowBo);
		int updated = paymentFlowMapper.updateByPrimaryKeySelective(paymentFlow);
		if (updated > 0) {
			return ServiceReturnUtils.success();
		}
		return ServiceReturnUtils.error(ServiceReturnCodes.PAYMENT_FLOW_UPDATE_ERROR);
	}

}
